<template>
	<el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px" class="login-container">
	    <h3 class="title">用餐管理系统登录</h3>
	    <el-form-item prop="EmployeeID">
	      	<el-input type="text" v-model="loginForm.EmployeeID" auto-complete="off" placeholder="工号"></el-input>
	    </el-form-item>
	    <el-form-item prop="Password">
	      	<el-input type="password" v-model="loginForm.Password" auto-complete="off" placeholder="密码"></el-input>
	    </el-form-item>
	    <el-checkbox v-model="rembAccount" checked class="remember" @click.native.prevent="fnRembAccount">记住账号</el-checkbox>
	    <el-form-item style="width:100%;">
	      	<el-button type="primary" style="width:100%;" @click.native.prevent="fnBtnLogin">登录</el-button>
	    </el-form-item>
	</el-form>
</template>

<script>
  	import Svc from '../service/index.js'
  	export default {
	    data() {
	      return {
	        loginForm: {
	          EmployeeID: '',
	          Password: ''
	        },
	        loginRules: {
	          EmployeeID: [
	            { required: true, message: '请输入员工账号', trigger: 'blur' }
	          ],
	          Password: [
	            { required: true, message: '请输入密码', trigger: 'blur' }
	          ]
	        },
	        //记住账号
	        rembAccount : true
	      };
	    },
	    methods: {
            //显示message提示框
            openMessage : function(msg,type){
    	        this.$message({
		          	showClose: true,
		          	message: msg,
		          	type: type
		        });
            },
			//记住密码
			fnRembAccount : function(){
				this.rembAccount = !this.rembAccount
			},
			//登录
		    fnBtnLogin(){
		    	var that = this
		        that.$refs.loginForm.validate((valid) => {
		          if (valid) {
			            //请求登录操作
			            Svc.fnPostData('/api/account/login', that.loginForm,function(res){
			            	if (res.body.ErrCode == 0){
			            		//记住账号
			            		if (that.rembAccount) {
			            			localStorage.setItem('rembAccount',that.loginForm.EmployeeID);
			            		} else {
			            			localStorage.setItem('rembAccount','');
			            		}
			            		//判断当前用户是否为后勤人员
			            		that.fnShowMenu(res.body.Data)
			            		//登录成功后跳转并保存用户信息
			            		sessionStorage.setItem('user', JSON.stringify(res.body.Data));
			            		// 判断是否为后勤登录，跳转到不同的待办事项页面
			            		if (res.body.Data.IsLogistics) {
                                    that.$router.push({ path: '/sign' });
								} else {
                                    that.$router.push({ path: '/mealmanagement' });
								}
			            	} else {
			            		that.openMessage(res.body.ErrMsg,'error')
			            	}
			            },function(res){
			            	that.openMessage("网络错误，请重新！",'error')
			            })
		          	} else {
			          	that.openMessage("账号及密码必须填写！",'error')
			            return false;
			        }
		        });
		    },
		    //判断菜单是否展示
		    fnShowMenu(data){
		    	var routes = this.$router.options.routes
		    	//当前用户为审核人
		    	if(data.IsApprover) {
		    		for(var i = 0; i < routes.length; i++) {
		    			var item = routes[i]
		    			if(item.iconCls ==="el-icon-custom-managerment"
		    				|| item.iconCls ==="el-icon-custom-apply"
		    				|| item.iconCls ==="el-icon-custom-my-managerment"
		    				|| item.iconCls ==="el-icon-custom-my-apply") {
		    				item.hidden = false
		    				var child = item.children || []
							for(var j = 0; j < child.length; j++) {
            					child[j].hidden = false
        					}
			        	}
        			}
		    	}
		    	
		    	//当前用户为管理员
		    	if(data.IsAdmin) {
		    		for(var i = 0; i < routes.length; i++) {
		    			var item = routes[i]
		    			if(item.path === "/home" && item.iconCls !== "el-icon-custom-managerment2") {
		    				item.hidden = false
		    				var child = item.children || []
							for(var j = 0; j < child.length; j++) {
            					child[j].hidden = false
        					}
        				}
        			}
		    	}
		    	
		    	//当前用户为后勤人员
		    	if(data.IsLogistics) {
		    		for(var i = 0; i < routes.length; i++) {
		    			var item = routes[i]
		    			if(item.iconCls ==="el-icon-custom-managerment2"
		    				|| item.iconCls ==="el-icon-custom-apply"
		    				|| item.iconCls ==="el-icon-custom-my-apply"
		    				|| item.iconCls ==="el-icon-custom-search"
		    				|| item.iconCls === "el-icon-custom-report") {
		    				item.hidden = false
		    				var child = item.children || []
							for(var j = 0; j < child.length; j++) {
            					child[j].hidden = false
        					}
			        	}
        			}
		    	}
		    	
		    	//当前用户非管理员、非审核人
		    	if (!data.IsAdmin && !data.IsApprover && !data.IsLogistics) {
		    		for(var i = 0; i < routes.length; i++) {
		    			var item = routes[i]
		    			if(item.iconCls ==="el-icon-custom-apply" || item.iconCls ==="el-icon-custom-my-apply") {
	        				item.hidden = false
		    				var child = item.children || []
							for(var j = 0; j < child.length; j++) {
            					child[j].hidden = false
        					}
		    			}
        			}
		    	}
		    }
	    },
		mounted() {
			//记住密码是否勾选
			var employeeID = localStorage.getItem('rembAccount')
			if (employeeID) {
				this.rembAccount = true
				this.loginForm.EmployeeID = employeeID
			} else {
				this.rembAccount = false
			}
		}
	}

</script>

<style scoped>
    .login-container {
	    -webkit-border-radius: 5px;
	    border-radius: 5px;
	    -moz-border-radius: 5px;
	    background-clip: padding-box;
	    margin: 180px auto;
	    width: 350px;
	    padding: 35px 35px 15px 35px;
	    background: #fff;
	    border: 1px solid #eaeaea;
	    box-shadow: 0 0 25px #cac6c6;
    }
  
	.login-container .title {
	   margin: 0px auto 40px auto;
	   text-align: center;
	   color: #505458;
	}
	.login-container .remember {
	   margin: 0px 0px 35px 0px;
	}
</style>